package com.muh.controller;

import com.muh.entity.User1;
import com.muh.service.User1Service;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * (User1)表控制层
 * 断路器
 * @author makejava
 * @since 2025-07-14 16:16:34
 */
@RestController
@RequestMapping("/circuitBreakerController")
public class CircuitBreakerController {
    /**
     * 服务对象
     */
    @Autowired
    private User1Service user1Service;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public ResponseEntity<User1> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.user1Service.queryById(id));
    }

    @RequestMapping("/getUserByName")
    @CircuitBreaker(name = "CircuitBreakerController", fallbackMethod = "fallbackMethodName")
    public List<User1> getUserByName(@RequestParam("name")String name) {
        List<User1> user1List= this.user1Service.getUserByName(name);
        try {
            User1 user1 = user1List.get(0);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return user1List;
    }
    public List<User1> fallbackMethodName(@RequestParam("name")String name, Exception ex) {
        List<User1> user1List= new ArrayList<>();
        User1 user1 = new User1();
        user1.setName("没有这个家伙\t"+name);
        user1List.add(user1);
        return user1List;
    }

}

